<?php

/**
 * The RegisterCore class is a controller of the Design Pattern.
 * 
 * @abstract LayoutPage
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class RegisterCore extends LayoutPage {

    public static $id = null;
    public static $activate = null;
    public static $cpf = null;
    public static $cnpj = null;
    public static $name, $email;
    
    public function setMedia() {
        parent::setMedia();
        
        Shell::addScript('jquery.maskedinput.min');
    }
    
    public function preProcess() {
        parent::preProcess();
        
        if (!empty($_GET['activate']) && !empty($_GET['type'])) {
            self::$activate = $_GET['activate'];
            $type = $_GET['type'];
            $data = array('active' => 1);
            if (DbSet::accounts($data, self::$activate, $type)) {
                header('Location: '.__BASE_URI__.'login');
                exit();
            }
        }
        elseif (!empty($_POST['email']) && !empty($_POST['password'])) {
            $account = DbGet::account($_POST['email'], md5($_POST['password']));
            if ($customer = DbGet::customer($account->id)) {
                self::$id = $customer->id;
                self::$name = $customer->name;
            }
            elseif ($enterprise = DbGet::enterprise($account->id)) {
                self::$id = $enterprise->id;
                self::$name = $enterprise->fancyname;
            }
            self::$email = $_POST['email'];
        }
        elseif (!empty($_POST['email']) && !empty($_POST['cpfcnpj'])) {
            self::$email = $_POST['email'];
            $cpfcnpj = preg_replace('/\D/s', '', $_POST['cpfcnpj']);
            if (strlen($cpfcnpj) < 13)
                self::$cpf = $cpfcnpj;
            else
                self::$cnpj = $cpfcnpj;
        }
        else {
            header('Location: '.__BASE_URI__.'login');
            exit();
        }
    }
    
    public function displayHeader() {
        global $_tag_title, $_metatag_description, $_metatag_keywords;
        
        if (Register::$activate != null) {
            $_tag_title = 'Erro de Ativação do Cadastro';
            $_metatag_description = 'Erro na tentativa de ativação do cadastro';
        }
        elseif (Register::$id == null) {
            $_tag_title = 'Cadastro do Cliente';
            $_metatag_description = 'Cadastrando dados pessoais do cliente';
        }
        else {
            $_tag_title = 'Cadastro não foi ativado';
            $_metatag_description = 'Cadastro do cliente não foi ativado';
        }
        $_tag_title .= ' | Goiautos.com.br';
        $_metatag_description .= ' no Goiautos, o site de classificados de veículos usados, seminovos e novos.';
        $_metatag_keywords = str_word_count(mb_strtolower('carros motos novos seminovos usados caminhões veículos classificados goiás cadastro dados pessoais ativação', 'UTF-8'), 1, 'áéíóúàâêôçãõ0123456789');
        
        parent::displayHeader();
    }
    
    public function process() {
        parent::process();
        
        self::$banners['lateral'] = Util::view_banners('lateral', 12);
    }
    
    public function displayContent() {
        parent::displayContent();
        
        Shell::addWidget(array('new-search','side-menus'), 'sidebar-left');
        Shell::showPage('register');
    }

}